﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using WMPLib;

namespace TSP_WMP
{
    public partial class TSP_WindowMeDiaPlayer : DevExpress.XtraEditors.XtraForm
    {
        #region Contractor
        public TSP_WindowMeDiaPlayer()
        {
            InitializeComponent();
        }
        #endregion

        #region Private Members
        string[] List = null;
        int location = 0;//
        #endregion

        #region Event
        private void barEditItem_EditValueChanged(object sender, EventArgs e)
        {
            defaultLookAndFeel.LookAndFeel.SkinName = barEditItem.EditValue.ToString();
        }

        private void TSP_WindowMeDiaPlayer_Load(object sender, EventArgs e)
        {
            foreach (SkinContainer skin in SkinManager.Default.Skins)
            {
                repositoryItemComboBox.Items.Add(skin.SkinName);
            }
            notifyIcon.Visible = false;
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            base.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon.Dispose();
            Application.Exit();
        }

        private void TSP_WindowMeDiaPlayer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                notifyIcon.Visible = true;
                notifyIcon.BalloonTipText = "Đã ẩn chương trình\nNếu muốn hiện nó lên\nBạn có thể double click vào icon";
                notifyIcon.ShowBalloonTip(1);
                e.Cancel = true;
                this.Hide();
            }
            else
            {
                notifyIcon.Dispose();
            }
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            base.Show();
        }
        private void lbPlaylist_DoubleClick(object sender, EventArgs e)
        {
            if (lbPlaylist.Items.Count <= 0)
            {
                ofdOpen.Filter = "All Files|*.*|Mp3 Files|*.Mp3|Wma Files|*.wma";
                ofdOpen.Title = "Chọn các bản nhạc";
                ofdOpen.Multiselect = true;
                if (ofdOpen.ShowDialog() == DialogResult.OK)
                {
                    lbPlaylist.Items.Clear();//Auto Clear Items on Playlist
                    //Add Items to Playlist
                    foreach (String media in ofdOpen.FileNames)
                    {
                        int ps = media.LastIndexOf('\\');
                        string str = media.Substring(ps + 1);
                        lbPlaylist.Items.Add(str);
                        //Status Total Items on Playlist
                        //navBarGroupListPanel.Caption = "Play List Total " + lbPlaylist.Items.Count + " items";
                        //location++;
                    }

                    if (axWindowsMediaPlayer.status.Contains("Stop"))//Autoplay when Stop after Add Playlist
                    {
                        axWindowsMediaPlayer.Ctlcontrols.play();
                    }

                    //Next Song from first to end
                    List = ofdOpen.FileNames;
                    Random ran = new Random();
                    for (int i = 0; i < List.Length; i++)
                    {
                        WMPLib.IWMPMedia f = this.axWindowsMediaPlayer.newMedia(List[i]);
                        this.axWindowsMediaPlayer.currentPlaylist.appendItem(f);
                        lbPlaylist.SelectedItem = lbPlaylist.Items[0];

                    }

                    this.axWindowsMediaPlayer.Ctlcontrols.play();
                    btnPlay.Text = "Pause";
                }
            }
            else
            {
                location = lbPlaylist.SelectedIndex;//Location Selected
                axWindowsMediaPlayer.URL = lbPlaylist.Items[location].ToString();//Play Song location selected Double Click 
                {
                    List = ofdOpen.FileNames;
                    for (int i = (location); i < List.Length; i++)//Next Song 
                    {
                        WMPLib.IWMPMedia f = this.axWindowsMediaPlayer.newMedia(List[i]);
                        this.axWindowsMediaPlayer.currentPlaylist.appendItem(f);
                    }
                    this.axWindowsMediaPlayer.Ctlcontrols.play();
                    btnPlay.Text = "Pause";
                }
            }
        }
        private void navBarItemOpen_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ofdOpen.Filter = "All Files|*.*|Mp3 Files|*.Mp3|Wma Files|*.wma";
                ofdOpen.Title = "Chọn các bản nhạc";
                ofdOpen.Multiselect = true;
                if (ofdOpen.ShowDialog() == DialogResult.OK)
                {
                    lbPlaylist.Items.Clear();//Auto Clear Items on Playlist
                    //Add Items to Playlist
                    foreach (String media in ofdOpen.FileNames)
                    {
                        int ps = media.LastIndexOf('\\');
                        string str = media.Substring(ps + 1);
                        lbPlaylist.Items.Add(str);
                        //Status Total Items on Playlist
                        //navBarGroupListPanel.Caption = "Play List Total " + lbPlaylist.Items.Count + " items";
                        //location++;
                    }

                    if (axWindowsMediaPlayer.status.Contains("Stop"))//Autoplay when Stop after Add Playlist
                    {
                        axWindowsMediaPlayer.Ctlcontrols.play();
                    }

                    //Next Song from first to end
                    List = ofdOpen.FileNames;
                    Random ran = new Random();
                    for (int i = 0; i < List.Length; i++)
                    {
                        WMPLib.IWMPMedia f = this.axWindowsMediaPlayer.newMedia(List[i]);
                        this.axWindowsMediaPlayer.currentPlaylist.appendItem(f);
                        lbPlaylist.SelectedItem = lbPlaylist.Items[0];

                    }

                    this.axWindowsMediaPlayer.Ctlcontrols.play();
                    btnPlay.Text = "Pause";
                }
        }
        private void btnPlay_Click(object sender, EventArgs e)
        {
            PlayPause_Click();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            Stop_Click();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            Previous_Click();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            Next_Click();
        }

        private void chkRepeat_CheckedChanged(object sender, EventArgs e)
        {
            if (chkRepeat.Checked == true)
                axWindowsMediaPlayer.settings.setMode("loop", true);
            else
                axWindowsMediaPlayer.settings.setMode("loop", false);
        }

        private void chkShuffle_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShuffle.Checked == true)
                axWindowsMediaPlayer.settings.setMode("shuffle", true);
            else
                axWindowsMediaPlayer.settings.setMode("shuffle", false);
        }

        private void zoomTrackBarControl_EditValueChanged(object sender, EventArgs e)
        {
            axWindowsMediaPlayer.settings.volume = zoomTrackBarControl.Value * 10;
        }
        #endregion

        #region Private Methods

        private void PlayPause_Click()
        {
            if (axWindowsMediaPlayer.status.Contains("Play"))
            {
                axWindowsMediaPlayer.Ctlcontrols.pause();

                btnPlay.Text = "Play";
            }
            else
            {
                axWindowsMediaPlayer.Ctlcontrols.play();

                btnPlay.Text = "Pause";
            }
        }

        private void Stop_Click()
        {
            axWindowsMediaPlayer.Ctlcontrols.stop();

        }

        private void Previous_Click()
        {
            location--;
            if (location >= 0)
            {
                lbPlaylist.SelectedItem = lbPlaylist.Items[location];
                axWindowsMediaPlayer.Ctlcontrols.previous();
            }

            else
            {
                btnPlay.Text = "Play";
                location = 0;
                axWindowsMediaPlayer.Ctlcontrols.stop();
                MessageBox.Show("Đã hết list không thể previous");
            }
        }

        private void Next_Click()
        {
            location++;
            if (location < List.Length)
            {
                axWindowsMediaPlayer.Ctlcontrols.next();
                lbPlaylist.SelectedItem = lbPlaylist.Items[location];
            }
            else
            {
                btnPlay.Text = "Play";
                location = List.Length - 1;
                axWindowsMediaPlayer.Ctlcontrols.stop();
                MessageBox.Show("Đã hết list không thể next");
            }
        }

        private void ClearPlayList()
        {
            for (int i = 0; i < axWindowsMediaPlayer.currentPlaylist.count; i++)
            {

                IWMPMedia med = axWindowsMediaPlayer.currentPlaylist.get_Item(i);

                axWindowsMediaPlayer.currentPlaylist.removeItem(med);
            }

            axWindowsMediaPlayer.Ctlcontrols.stop();
            lbPlaylist.Items.Clear();

        }
        #endregion

       

        

    }
}